<template>
    <div class="container-fluid">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item>          
            <el-input v-model="article.name" placeholder="请输入文章标题"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="article.content" placeholder="请输入文章内容"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addEdit">添加文章</el-button>
          </el-form-item>
         <!--<el-input placeholder="请输入内容" v-model="input3" class="input-with-select" style="width:200px">
            <el-button slot="append" icon="el-icon-search"></el-button>
         </el-input>-->         
          <el-form-item>
            <el-button type="primary" @click="query">刷新</el-button>
          </el-form-item>
         </el-form> 
        <template>
          <el-table :data="list"  height="250"  border style="width: 100%">
            <el-table-column prop="id" label="编号" width="425">
              <template slot-scope="scope">
                {{scope.$index +1}}
              </template>
            </el-table-column>
            <el-table-column prop="name" label="文章标题" width="430"></el-table-column>
            <el-table-column prop="content" label="文章内容" width="400"></el-table-column>
            <el-table-column fixed="right" label="操作" width="120">
                <template slot-scope="scope">
                  <el-button @click.native.prevent="deleteRow(scope.$index ,scope.row)" type="text" size="small">移除</el-button>
                </template>
            </el-table-column>
          </el-table>
        </template>
    </div>
</template>
<script>
  export default {
    created(){
      this.query();
    },
    methods: {
      deleteRow(id,row) {
        let url =`http://localhost:3000/article/` +row.id;
        this.axios.delete(url).then((response)=>{
          console.log(response.data);
          this.list = response.data;
        
       });
        this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then((response) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
         this.query();
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
  
      },
      onSubmit() {
        console.log('submit!');
      },
      addEdit(){//编写文章
        let url =`http://localhost:3000/article` ;
        this.axios.post(url,{
          name:this.article.name,
          content:this.article.content
        }).then((response)=>{
        console.log(response)
       });
        if(!this.article.name){
          this.$message({
          message: '请输入文章标题',
          type: 'warning'
        });
        return;
        }
        if(!this.article.content){
          this.$message({
          message: '请输入文章内容',
          type: 'warning'
        });
        return;
        }
        this.list.push(this.article);
        this.article=[]

      },
      query(){
       this.axios("http://localhost:3000/article",{
        
       }).then((response)=>{
          console.log(response.data);
          this.list = response.data;
       })
      }
      },
    data() {
      return {
        drawer: false,
        formInline: {
          user: '',
         region: ''
        },
         list: [],
         article: []
        
      }
    }
    }
</script>